<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="35%">
    <el-form :model="params" class="demo-form-inline" ref="params" :rules="rules" label-position="top">
      <el-row :gutter="20">
        <el-col :span="24" v-if="flag">
          <el-form-item :label="$t('config.supplierType.upName')" prop="parentId">
            <x-select-tree
              v-model="params.parentId"
              ref="selectTree"
              :options="treeData"
              :placeholder="$t('config.supplierType.upNamePlaceholder')"
              :defaultProps="treeProps"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.supplierType.name')" prop="supplierTypeName">
            <el-input
              v-model="params.supplierTypeName"
              :placeholder="$t('config.supplierType.namePlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.supplierType.code')" prop="supplierTypeCode">
            <el-input
              v-model="params.supplierTypeCode"
              :placeholder="$t('config.supplierType.codePlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.supplierType.siteReview')" prop="siteReview">
            <x-radio-group :list-data="dictOptions['system.preset']" v-model="params.siteReview"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.supplierType.detectMaterial')" prop="detectMaterial">
            <x-radio-group :list-data="dictOptions['system.preset']" v-model="params.detectMaterial"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.supplierType.isOpen')" prop="state">
            <x-radio-group :list-data="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.supplierType.remark')" prop="remark">
            <el-input
              v-model="params.remark"
              type="textarea"
              :rows="3"
              :placeholder="$t('config.supplierType.remarkPlaceholder')"
              :autosize="{ minRows: 2, maxRows: 8 }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button type="primary" @click="handleSubmit" :loading="btnLoading"> {{ $t('common.confirm') }} </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiGetInfo, apiEditSupplierType, apiAddSupplierType } from '../api/supplierType'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    props: {
      treeData: {
        type: Array,
        default: () => []
      },
      treeProps: {
        type: Object,
        default: () => ({})
      },
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        btnLoading: false,
        flag: true,
        params: {
          parentId: '0',
          supplierTypeName: '',
          supplierTypeCode: '',
          state: '1',
          siteReview: '1',
          detectMaterial: '1',
          remark: ''
        },
        rules: {
          parentId: [{ required: true, message: this.$t('config.supplierType.upNamePlaceholder'), trigger: 'blur' }],
          supplierTypeName: [
            { required: true, message: this.$t('config.supplierType.namePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('config.supplierType.lengthTip'), trigger: 'blur' }
          ],
          supplierTypeCode: [
            { required: true, message: this.$t('config.supplierType.codePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('config.supplierType.lengthTip'), trigger: 'blur' }
          ],
          siteReview: [{ required: true, message: this.$t('config.supplierType.selectPlaceholder'), trigger: 'blur' }],
          detectMaterial: [
            { required: true, message: this.$t('config.supplierType.selectPlaceholder'), trigger: 'blur' }
          ],
          state: [{ required: true, message: this.$t('config.supplierType.selectPlaceholder'), trigger: 'blur' }]
        }
      }
    },
    methods: {
      handleOpen(info, flag = true) {
        this.visible = true
        this.params.parentId = info?.parentId ?? 0
        this.flag = flag && `${info.parentId}` !== '0'
        if (info?.id) {
          this.title = this.$t('config.supplierType.editType')
          this.getInfo(info.id)
          return
        }
        this.title = this.$t('config.supplierType.addType')
      },
      async getInfo(id) {
        const { data, code } = await apiGetInfo(id)
        if (code === EnumApiCode.Success) {
          this.$nextTick(() => {
            this.params = Object.assign(this.params, cloneDeep(data))
            this.params.state += ''
            this.params.siteReview += ''
            this.params.detectMaterial += ''
          })
        }
      },
      handleSubmit: throttle(function () {
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            this.btnLoading = true
            const param = {
              ...this.params,
              parentId: typeof this.params.parentId === 'object' ? this.params.parentId.value : this.params.parentId
            }
            const { code, msg } =
              'id' in this.params ? await apiEditSupplierType(param) : await apiAddSupplierType(param)
            if (code === EnumApiCode.Success) {
              this.btnLoading = false
              this.$message.success(param.id ? this.$t('common.editSuccess') : this.$t('common.newSuccess'))
              this.$emit('handleSuccess')
              this.handleClose()
            } else {
              this.btnLoading = false
              this.$message.error(msg)
            }
          } else {
            this.btnLoading = false
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      }
    }
  }
</script>
